package com.hyfd.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hyfd.common.https.HttpsUtils;
import com.hyfd.common.utils.MD5;
import com.hyfd.common.utils.XmlUtils;
import com.hyfd.dao.mp.OrderDao;
import com.hyfd.dao.mp.ProviderPhysicalChannelDao;
import com.hyfd.rabbitMq.RabbitMqProducer;
import com.hyfd.rabbitMq.SerializeUtil;

@Component
public class FuYuanFlowTask {

	@Autowired
	ProviderPhysicalChannelDao providerPhysicalChannelDao;//物理通道信息
	@Autowired
	OrderDao orderDao;//订单
	@Autowired
	RabbitMqProducer mqProducer;//消息队列生产者
	private static Logger log = Logger.getLogger(FuYuanFlowTask.class);
	
	
//	@Scheduled(fixedDelay = 60000)
	public void queryFuYuanOrder(){
		Map<String,Object> map = new HashMap<String,Object>();

		int flag = -1;
		try{
			String id = "0000000100";
			Map<String,Object> channel = providerPhysicalChannelDao.selectByPrimaryKey(id);//获取通道的数据
			String defaultParameter = (String) channel.get("default_parameter");//默认参数
			Map<String,String> paramMap = XmlUtils.readXmlToMap(defaultParameter);
			
			String queryUrl = (String)channel.get("query_url");// 地址
			String agentid = paramMap.get("agentid"); // 渠道商编号 
			String key = paramMap.get("key"); // 渠道商编号 
			String action = "query";
			    
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("dispatcherProviderId", id);
			param.put("status", "1");
			
			List<Map<String,Object>> orderList = orderDao.selectByTask(param);
			
			for(Map<String,Object> order : orderList){

				String orderId = order.get("orderId")+"";
				map.put("orderId", orderId);
				
				//String value = order.get("providerOrderId")+"";
				
				StringBuilder postData = new StringBuilder();
				postData.append("agentid").append("=").append(agentid).append("&");
				postData.append("orderid").append("=").append(orderId).append("&");
				postData.append("key").append("=").append(key);
				String data = postData.toString();
				
				String sign = MD5.ToMD5(data).toLowerCase();
				
				StringBuilder postMsg = new StringBuilder();
				postMsg.append("agentid").append("=").append(agentid).append("&");
				postMsg.append("orderid").append("=").append(orderId).append("&");
				postMsg.append("action").append("=").append(action).append("&");
				postMsg.append("sign").append("=").append(sign);
				
				String retMsg = HttpsUtils.postParameters(queryUrl, postMsg.toString());
				
				if (retMsg == null) {
			          log.error("福缘流量查询job返回值为空 orderids=" + orderId);
			      }else {
			    	  Map<String,String> resultMap = new HashMap<String, String>();
			    	  resultMap = (Map<String, String>) JSONObject.parse(retMsg);
			    	  String result = resultMap.get("status");
			    	  String state = resultMap.get("status");
			    	  if("Success".equals(result)){
			    		  if("3".equals(state)){
			    			  flag = 1;
			    		  }else if ("4".equals(state)) {
			    			  flag = 1;
			    		  }else {
							continue;
						}
			    		  
			    	  }else {
			    		  continue;
			    	  }
			    	  map.put("status", flag);
			    	  mqProducer.sendDataToQueue(RabbitMqProducer.Result_QueueKey, SerializeUtil.getStrFromObj(map));
				}
			}
		}catch(Exception e){
			log.error("福缘查询Task出错"+e);
		}
	}	
	
}
